package br.affero.core.person.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "person")
public class Person implements Serializable {
        private static final long serialVersionUID = 1L;
        
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name = "person_id")
        private Integer personId;
        @NotNull
        private String name;
        @Column(name="birth_date")
        @NotNull
        private Date birthDate;
        private Boolean active;
        
        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        @JoinTable(name="person_experience", joinColumns = { @JoinColumn(name="person_id") }, 
                                      inverseJoinColumns = {@JoinColumn(name="experience_id")})
        private List<Experience> personExperiences = new ArrayList<Experience>();

        public Integer getPersonId() {
                return personId;
        }

        public void setPersonId(Integer personId) {
                this.personId = personId;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }
        

        public Date getBirthDate() {
                return birthDate;
        }

        public void setBirthDate(Date birthDate) {
                this.birthDate = birthDate;
        }

        public Boolean getActive() {
                return active;
        }

        public void setActive(Boolean active) {
                this.active = active;
        }
        
        public void toggleActive() {
                this.active = this.active == null ? true : !this.active;
        }

        public List<Experience> getPersonExperiences() {
                return personExperiences;
        }

        public void setPersonExperiences(List<Experience> personExperiences) {
                this.personExperiences = personExperiences;
        }
        

        @Override
        public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + ((active == null) ? 0 : active.hashCode());
                result = prime * result
                                + ((birthDate == null) ? 0 : birthDate.hashCode());
                result = prime * result + ((name == null) ? 0 : name.hashCode());
                result = prime * result
                                + ((personId == null) ? 0 : personId.hashCode());
                return result;
        }

        @Override
        public boolean equals(Object obj) {
                if (this == obj)
                        return true;
                if (obj == null)
                        return false;
                if (getClass() != obj.getClass())
                        return false;
                Person other = (Person) obj;
                if (active == null) {
                        if (other.active != null)
                                return false;
                } else if (!active.equals(other.active))
                        return false;
                if (birthDate == null) {
                        if (other.birthDate != null)
                                return false;
                } else if (!birthDate.equals(other.birthDate))
                        return false;
                if (name == null) {
                        if (other.name != null)
                                return false;
                } else if (!name.equals(other.name))
                        return false;
                if (personId == null) {
                        if (other.personId != null)
                                return false;
                } else if (!personId.equals(other.personId))
                        return false;
                return true;
        }

        @Override
        public String toString() {
                StringBuilder builder = new StringBuilder();
                builder.append("Person [personId=");
                builder.append(personId);
                builder.append(", name=");
                builder.append(name);
                builder.append(", birthDate=");
                builder.append(birthDate);
                builder.append(", active=");
                builder.append(active);
                builder.append(", personExperiences=");
                builder.append(personExperiences);
                builder.append("]");
                return builder.toString();
        }
}